/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.trader.utils;

import javax.servlet.http.HttpServletRequest;

/**
 * Utility functions to get info about the webapges
 * @author Ahsan
 */
public class URLUtils
{
	 /**
	  * Gets the main webpage (e.g. "http://localhost:8080/Trader")
	  * @param request the servlet request
	  * @return the main webpage
	  */
	 public static String getMainWebpage(HttpServletRequest request)
	 {
		  return "http://" + getMainURL(request);
	 }

	 /**
	  * Gets the URL to the main website (e.g. "localhost:8080/Trader")
	  * @param request the servlet request
	  * @return the main URL
	  */
	 public static String getMainURL(HttpServletRequest request)
	 {
		  String host = request.getHeader("Host");

		  String pageURI = request.getRequestURI();

		  int indexOfSlash = pageURI.lastIndexOf('/');
		  if (indexOfSlash > 0)
		  {
			   pageURI = pageURI.substring(0, indexOfSlash);
		  }

		  return host + pageURI;
	 }

}
